package com.nttuan.mycalender;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.nttuan.adapter.NotificationAdapter;
import com.nttuan.model.dao.TrackDao;
import com.nttuan.model.dao.service.TrackService;
import com.nttuan.model.entity.Track;
import com.nttuan.util.LogSupport;
import com.nttuan.util.Support;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * Created by TuanNT on 11/27/2014.
 */
public class NotificationActivity extends Activity implements AdapterView.OnItemClickListener{
    private final int DELAY = 10000;
    private final int OFFSET_LOAD = 1440; // 30p
    private final String TAG = "NotificationActivity";

    private ListView listView;
    private ProgressBar progressBar;
    private NotificationAdapter adapter;
    private TrackService trackService;
    private List<Track> tracks =new ArrayList<Track>();
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        listView =(ListView)findViewById(R.id.listViewNotification);
        progressBar =(ProgressBar)findViewById(R.id.progressBarNotifi);
        trackService = TrackDao.getInstance(this);
        adapter = new NotificationAdapter(this, tracks);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }


    private Runnable updateNotifi = new Runnable() {
        @Override
        public void run() {
            Date date = LogSupport.lastTimeNotifi(NotificationActivity.this);
            List<Track> list =  trackService.tracks(date);
            if(list.size() > 0)
                LogSupport.saveLastTimeNotifi(NotificationActivity.this, list.get(0).getLastTime());
            for(int i = list.size() - 1; i >= 0; i--)
                    tracks.add(0,list.get(i));
            adapter.notifyDataSetChanged();
            handler.postDelayed(this,DELAY);
        }
    };
    private void stopUpdate(){
        if(handler != null)
            handler.removeCallbacks(updateNotifi);
    }
    private void startUpdate(){
        if(handler!=null)
            handler.postAtTime(updateNotifi,DELAY);
    }
    class loadTrack extends AsyncTask<Void, Void, Void>{
        private Context context;

        public loadTrack(Context context) {
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            progressBar.setVisibility(View.GONE);
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            Date lastUpdate = LogSupport.lastTimeNotifi(context);
            adapter.setSavePoint(lastUpdate);
            tracks = trackService.tracks(Support.subDate(lastUpdate, OFFSET_LOAD));
            if(tracks.size() > 0)
                LogSupport.saveLastTimeNotifi(context, tracks.get(0).getLastTime());

            adapter.setList(tracks);
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            adapter.notifyDataSetChanged();
            progressBar.setVisibility(View.GONE);
            startUpdate();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        new loadTrack(this).execute();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopUpdate();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Bundle bundle = new Bundle();
        Track track = tracks.get(i);
        switch (track.getActon()){
            case Track.ACTION_RECEIVE_INVITE:
                bundle.putInt(AddNewActivity.KEY_ACTION,AddNewActivity.KEY_ACTION_VIEW_INVITE);
                break;
            case Track.ACTION_RECEIVE_UPDATE_INVITE:
                bundle.putInt(AddNewActivity.KEY_ACTION,AddNewActivity.KEY_ACTION_VIEW_INVITE);
                break;
            default:
                bundle.putInt(AddNewActivity.KEY_ACTION,AddNewActivity.KEY_ACTION_VIEW);
                break;
        }
        bundle.putInt(AddNewActivity.KEY_TASK_ID,track.getTask().getTaskId());
        Intent intent = new Intent(this, AddNewActivity.class);
        intent.putExtra(AddNewActivity.KEY_BUNDLE, bundle);
        startActivity(intent);
    }
}
